package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.domain.MetaverseShop;
import com.ruoyi.shop.service.IMetaverseShopService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 店铺Controller
 * 
 * @author ruoyi
 * @date 2023-10-30
 */
@RestController
@RequestMapping("/metaverse-shop/shop")
public class MetaverseShopController extends BaseController
{
    @Autowired
    private IMetaverseShopService metaverseShopService;

    /**
     * 查询店铺列表
     */
    @PreAuthorize("@ss.hasPermi('metaverse-shop:shop:list')")
    @GetMapping("/list")
    public TableDataInfo list(MetaverseShop metaverseShop)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long groupId = user.getHostId();
        if (groupId!=null){
            List<MetaverseShop> shopList = metaverseShopService.selectMetaverseShopListAop(groupId);
            if(!shopList.isEmpty()){
                for (MetaverseShop shop: shopList){
                    if (shop.getGroupId() == groupId){
                        startPage();
                        List<MetaverseShop> list = metaverseShopService.selectMetaverseShopListAop(shop.getGroupId());
                        return getDataTable(list);
                    }
                }
            }else {
                return null;
            }
        }
        startPage();
        List<MetaverseShop> list = metaverseShopService.selectMetaverseShopList(metaverseShop);
        return getDataTable(list);
    }

    /**
     * 导出店铺列表
     */
    @PreAuthorize("@ss.hasPermi('metaverse-shop:shop:export')")
    @Log(title = "店铺", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MetaverseShop metaverseShop)
    {
        List<MetaverseShop> list = metaverseShopService.selectMetaverseShopList(metaverseShop);
        ExcelUtil<MetaverseShop> util = new ExcelUtil<MetaverseShop>(MetaverseShop.class);
        util.exportExcel(response, list, "店铺数据");
    }

    /**
     * 获取店铺详细信息
     */
    @PreAuthorize("@ss.hasPermi('metaverse-shop:shop:query')")
    @GetMapping(value = "/{shopId}")
    public AjaxResult getInfo(@PathVariable("shopId") Long shopId)
    {
        return success(metaverseShopService.selectMetaverseShopByShopId(shopId));
    }


    /**
     * 新增店铺
     */
    @PreAuthorize("@ss.hasPermi('metaverse-shop:shop:add')")
    @Log(title = "店铺", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MetaverseShop metaverseShop)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long groupId = user.getHostId();
        metaverseShop.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        metaverseShop.setGroupId(groupId);
        return toAjax(metaverseShopService.insertMetaverseShop(metaverseShop));
    }

    /**
     * 修改店铺
     */
    @PreAuthorize("@ss.hasPermi('metaverse-shop:shop:edit')")
    @Log(title = "店铺", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MetaverseShop metaverseShop)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        metaverseShop.setUpdateBy(user.getUserName());
        return toAjax(metaverseShopService.updateMetaverseShop(metaverseShop));
    }

    /**
     * 删除店铺
     */
    @PreAuthorize("@ss.hasPermi('metaverse-shop:shop:remove')")
    @Log(title = "店铺", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shopIds}")
    public AjaxResult remove(@PathVariable Long[] shopIds)
    {
        return toAjax(metaverseShopService.deleteMetaverseShopByShopIds(shopIds));
    }
}
